using System;
using System.Collections.Generic;
using System.IO;
using UnityEngine;
using System.Text.RegularExpressions;
/*
*Creater:Hyx
*Introduce:介绍
*Version:0.0.1
*ChangeLog:创建--2024.01.01
*/


public partial class CommonUtils
{
    /// <summary>
    /// UNITY 不支持的图片格式,不区分大小写
    /// </summary>
    public static string[] NotSupport_PicType = { "webp", "GIF", "svg", "HEIF", "HEIC" };

    /// <summary>
    /// 判断是不是UNITY 不支持的图片格式，支持返回true，不支持返回false
    /// </summary>
    /// <param name="picType"></param>
    /// <returns></returns>
    public static bool Is_Support_PicType(string picType)
    {
        for (int i = 0; i < NotSupport_PicType.Length; i++)
        {
            if (picType.Equals(NotSupport_PicType[i], StringComparison.OrdinalIgnoreCase))
            {
                return false;
            }
        }
        return true;
    }

    /// <summary>
    /// 字符串转Vector3
    /// </summary>
    /// <param name="str"></param>
    /// <returns></returns>
    public static Vector3 ChangeStringToVector3(string str)
    {
        if (!string.IsNullOrEmpty(str))
        {
            string[] arr = str.Split(',');
            return new Vector3(float.Parse(arr[0]), float.Parse(arr[1]), float.Parse(arr[2]));
        }
        Debug.LogError($"str is null");
        return Vector3.one;
    }


    /// <summary>
    /// 判断是否点击到了UI层
    /// </summary>
    /// <returns></returns>
    public static bool CheckClickOnUI()
    {
        bool b = false;
        return b;
    }

    public static void SetLayerRecursively(GameObject parentObj, int layerNumber)
    {
        parentObj.layer = layerNumber;

        foreach (Transform child in parentObj.transform)
        {
            SetLayerRecursively(child.gameObject, layerNumber);
        }
    }



    /// <summary>
    /// 图片转换成base64编码文本(传路径)
    /// </summary>
    public static string ImgToBase64String(string path)
    {
        string recordBase64String;
        try
        {
            FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read);
            byte[] buffer = new byte[fs.Length];
            fs.Read(buffer, 0, (int)fs.Length);
            string base64String = Convert.ToBase64String(buffer);
            // Debug.Log("获取当前图片base64为---" + base64String);
            recordBase64String = base64String;
            return recordBase64String;
        }
        catch (Exception e)
        {
            Debug.Log("ImgToBase64String 转换失败:" + e.Message);
            return null;
        }
    }

    /// <summary>
    /// base64编码文本转换成图片
    /// </summary>
    public static Sprite Base64ToImg(string recordBase64String)
    {
        //删掉前缀
        string base64 = recordBase64String.Replace("data:image/png;base64,", "");

        byte[] bytes = Convert.FromBase64String(base64);
        Texture2D tex2D = new Texture2D(400, 300);
        tex2D.LoadImage(bytes);
        Sprite s = Sprite.Create(tex2D, new Rect(0, 0, tex2D.width, tex2D.height), new Vector2(0.5f, 0.5f));

        //imgComponent.sprite = s;
        Resources.UnloadUnusedAssets();
        return s;
    }

    private static string phoneNumberPattern = @"^1\d{10}$";

    /// <summary>
    /// 检查输入合法性,检查是否是中国的手机号
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    public static string IsPhoneNum(string input)
    {

        if (Regex.IsMatch(input, phoneNumberPattern))
        {
            return "mobile";
        }
        else
        {
            return "请检查输入是否有误";
        }
    }



    /// <summary>
    /// 将texture转成image
    /// </summary>
    /// <param name="tex"></param>
    /// <returns></returns>
    public static Sprite GetSpriteByTexture(Texture2D tex)
    {
        Sprite _sprite = Sprite.Create(tex, new Rect(0, 0, tex.width, tex.height), new Vector2(0.5f, 0.5f));
        return _sprite;
    }



    public static EntityNum[] StrsplitStrArr(string str)
    {
        string[] strs = str.Split(',');
        EntityNum[] entityNums = new EntityNum[strs.Length];

        for (int i = 0; i < strs.Length; i++)
        {
            var strs2 = strs[i].Split('_');

            entityNums[i] = new EntityNum();

            entityNums[i].id = int.Parse(strs2[0]);
            if (strs2.Length == 2)
                entityNums[i].num = float.Parse(strs2[1]);
        }
        return entityNums;
    }


    public static int[] StrsplitIntArr(string str)
    {
        string[] strs = str.Split(',');
        int[] ints = new int[strs.Length];

        for (int i = 0; i < strs.Length; i++)
        {
            ints[i] = int.Parse(strs[i]);
        }
        return ints;
    }

    /// <summary>
    /// 根据权重选择一个道具
    /// </summary>
    /// <param name="entityNums"></param>
    /// <returns></returns>
    public static EntityNum GetRandomItemBasedOnWeight(EntityNum[] entityNums)
    {

        if (entityNums.Length == 1)
            return entityNums[0];


        // 计算所有权重的总和
        float totalWeight = 0f;
        foreach (var item in entityNums)
        {
            totalWeight += item.num;
        }

        // 生成一个0到totalWeight之间的随机数
        float randomWeight = UnityEngine.Random.Range(0f, totalWeight);

        // 根据随机数选择道具
        float cumulativeWeight = 0f;
        foreach (var item in entityNums)
        {
            cumulativeWeight += item.num;
            if (randomWeight <= cumulativeWeight)
            {
                return item;
            }
        }

        return null; // 如果没有选择任何道具，返回null
    }

    
    /// <summary>
    /// 根据权重选择一个道具
    /// </summary>
    /// <param name="entityNums"></param>
    /// <returns></returns>
    public static EntityNum GetRandomItemBasedOnWeight(List< EntityNum> entityNums)
    {

        if (entityNums.Count == 1)
            return entityNums[0];


        // 计算所有权重的总和
        float totalWeight = 0f;
        foreach (var item in entityNums)
        {
            totalWeight += item.num;
        }

        // 生成一个0到totalWeight之间的随机数
        float randomWeight = UnityEngine.Random.Range(0f, totalWeight);

        // 根据随机数选择道具
        float cumulativeWeight = 0f;
        foreach (var item in entityNums)
        {
            cumulativeWeight += item.num;
            if (randomWeight <= cumulativeWeight)
            {
                return item;
            }
        }

        return null; // 如果没有选择任何道具，返回null
    }


    /// <summary>
    /// 生成一个指定范围内的随机浮点数
    /// </summary>
    /// <param name="min"></param>
    /// <param name="max"></param>
    /// <returns></returns>
    public static float GenerateRandomNumber(float min, float max)
    {
        // 设置随机种子
        UnityEngine.Random.InitState(123);

        // 生成随机浮点数
        return UnityEngine.Random.Range(min, max);
    }

    /// <summary>
    /// 生成一个指定范围内的随机整数
    /// </summary>
    /// <param name="min"></param>
    /// <param name="max"></param>
    /// <returns></returns>
    public static int GenerateRandomNumber(int min, int max)
    {
        // 设置随机种子
        UnityEngine.Random.InitState(123);

        // 生成随机浮点数
        return UnityEngine.Random.Range(min, max);
    }


    // 限制角度在最小值和最大值之间
    public static float ClampAngle(float lfAngle, float lfMin, float lfMax)
    {
        // 如果角度小于-360度，则加上360度
        if (lfAngle < -360f) lfAngle += 360f;
        // 如果角度大于360度，则减去360度
        if (lfAngle > 360f) lfAngle -= 360f;
        // 返回限制在最小值和最大值之间的角度
        return Mathf.Clamp(lfAngle, lfMin, lfMax);
    }




}

public class EntityNum
{
    public int id;
    public float num;

}